package top.maof.book.spider.develop.book.zongheng;

import top.maof.book.model.Book;
import top.maof.book.model.Contents;
import top.maof.book.spider.develop.Split;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.model.AfterExtractor;
import us.codecraft.webmagic.model.annotation.ExtractBy;
import us.codecraft.webmagic.model.annotation.HelpUrl;
import us.codecraft.webmagic.model.annotation.TargetUrl;

import java.util.Date;

@TargetUrl("http://huayu.zongheng.com/book/\\d+.html")
@HelpUrl(value = {"http://book.zongheng.com/store.html", "http://book.zongheng.com/store[\\w\\?=/&.]*", "http://www.zongheng.com/rank[\\w\\?=/&.]*",
        "http://www.zongheng.com/rank.html"})
public class HuaYu implements AfterExtractor, Split {

    public static final String CHARSET = "utf-8";

    @ExtractBy("//div[@class=book-info]/div[@class=book-name]/text()")
    private String name;

    @ExtractBy("//div[@class=book-author]/div[@class=au-name]/a/text()")
    private String author;

    @ExtractBy("//div[@class=book-img]/img/@src")
    private String img;

    @ExtractBy("//div[@class=book-label]/a[2]/text()")
    private String classify;

    @ExtractBy("//div[@class=book-label]/a[1]/text()")
    private String state;

    /**
     * 书收录时间
     */
    private Date insertDate;

    /**
     * 最新章节更新时间
     */
    private Date updateDate;

    /**
     * 书源站点名字
     */
    private String sourceSite;

    /**
     * 书源站点地址
     */
    private String sourceUrl;

    /**
     * 书开始连载时间
     */
    private Date startDate;
    @ExtractBy("//div[@class=book-dec]/p[1]/text()")
    private String synopsis;

    public static String getCHARSET() {
        return CHARSET;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getImg() {
        return img;
    }

    public void setImg(String img) {
        this.img = img;
    }

    public String getClassify() {
        return classify;
    }

    public void setClassify(String classify) {
        this.classify = classify;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public Date getInsertDate() {
        return insertDate;
    }

    public void setInsertDate(Date insertDate) {
        this.insertDate = insertDate;
    }

    public Date getUpdateDate() {
        return updateDate;
    }

    public void setUpdateDate(Date updateDate) {
        this.updateDate = updateDate;
    }

    public String getSourceSite() {
        return sourceSite;
    }

    public void setSourceSite(String sourceSite) {
        this.sourceSite = sourceSite;
    }

    public String getSourceUrl() {
        return sourceUrl;
    }

    public void setSourceUrl(String sourceUrl) {
        this.sourceUrl = sourceUrl;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public String getSynopsis() {
        return synopsis;
    }

    public void setSynopsis(String synopsis) {
        this.synopsis = synopsis;
    }


    @Override
    public Book getBook() {
        return new Book(null, name, author, img, classify, state,
                insertDate, updateDate, sourceSite, sourceUrl, startDate, synopsis);
    }

    @Override
    public Contents getContents() {
        return null;
    }

    @Override
    public void afterProcess(Page page) {
        this.author = this.author.trim();
        this.name = this.name.trim();
        this.sourceSite = "纵横中文网";
        this.sourceUrl = page.getUrl().toString();
    }
}